<?php
/*
	*	@package joomla component
	*	model hello 
	*	component/com_hello/site/models/hello.php
	*	@license	GNU/GPL
*/
//không cho phép truy cập trực tiếp
defined('_JEXEC') or die('Restricted Access');

//import thư viện model cho component
//các thư viện cho component có thể tìm thấy ở /libraries/joomla/application/component
jimport('joomla.application.component.model');

//cách thông thường đặt tên cho các model trong joomla là : Tên Component + 'Model' + Tên Model

//Lop phan trang
class Pager{
	//tim vi tri bat dau
	function findStart($limit)
	{
		if($_GET['page']==0) 
		{
			$start=0;
			$_GET['page']=1;
            
		}
		else
		{
			$start= ($_GET['page'] - 1) * $limit;
            //$start+=$limit;
		}	
		return $start;
	}
    
	//tinh tong so trang
	function findPages($count, $limit)
	{
		if(($count%$limit)==0)
			$pages=$count/$limit;
		else
			$pages= floor($count/$limit) + 1;
		//$pages=(($count % $limit)==0 ? $count/$limit : (floor($count/$limit)+1);
		return $pages;
	}
	
	function pageList($curpage, $pages)
	{
		$page_list="";
		if(($curpage!=1) && ($curpage))
		{
			$page_list.="<a href=\"".$_SERVER['PHP_SELF']."?option=com_cuusinhvien&view=cuusinhvien&Itemid=27&page=1\" title=\"Trang dau\"> Trang dau </a>";
		}
		if(($curpage - 1)>0)
		{
			$page_list.="<a href=\"".$_SERVER['PHP_SELF']."?option=com_cuusinhvien&view=cuusinhvien&Itemid=27&page=".($curpage - 1)."\ title=\"Ve trang truoc\"> < </a>";
		}
		
		for($i=1; $i<$pages; $i++)
		{
			if($i == $curpage)
			{
				$page_list.="<b>".$i."</b>";
			}
			else
			{
				$page_list.="<a href=\"".$_SERVER['PHP_SELF']."?option=com_cuusinhvien&view=cuusinhvien&Itemid=27&page=".$i."\" title=\"Trang ".$i."\">".$i."</a>";
			}
			$page_list.=" ";
		}
		
		if(($curpage+1) <= $pages)
		{
			$page_list.="<a href=\"".$_SERVER['PHP_SELF']."?option=com_cuusinhvien&view=cuusinhvien&Itemid=27&page=".($curpage + 1)."\" title=\"Den trang sau\"> > </a>";;
		}
		if(($curpage!=$pages) && ($pages!=0))
		{
			$page_list.="<a href=\"".$_SERVER['PHP_SELF']."?option=com_cuusinhvien&view=cuusinhvien&Itemid=27&page=".$pages."\" title=\"Trang cuoi\"> Trang cuoi </a>";;
		}
		
		//$page_list.="</td>\n";
		
		return $page_list;
	}
	
	function nextPrev($curpage,$pages)
	{
		$next_prev="";
		if(($curpage-1)<=0)
		{
			$next_prev.="Về trang trước";
		}
		else
		{
			$next_prev.="<a href=\"".$_SERVER['PHP_SELF']."?page=".($curpage-1)."\">Về trang trước</a>";
		}
		$next_prev.=" | ";
		if(($curpage+1)>$pages)
			$next_prev.="Đến trang sau";
		else
		{
			$next_prev.="<a href=\"".$_SERVER['PHP_SELF']."?page=".($curpage-1)."\">Đến trang sau</a>";
		}
		return $next_prev;
	}
}


class CuuSinhVienModelCuuSinhVien extends JModel
{
	 var $_allData;
	 var $_total = null;
	 /**
	 * Constructor that retrieves the ID from the request
	 *
	 * @access	public
	 * @return	void
	 */
	 function __construct()
	  {
		parent::__construct();
	 
		global $mainframe, $option;

		//Method getUserStateFromRequest($key,$request,$default,$type) : lay gtri bien trang thai nguoi dung
		$filter_order= $mainframe->getUserStateFromRequest($option.'filter_order', 'filter_order', 'default_column_name', 'cmd' );
		//lay gtri bien trang thai nguoi dung
		$filter_order_Dir = $mainframe->getUserStateFromRequest( $option.'filter_order_Dir', 'filter_order_Dir', 'asc', 'word' );
		$filter_nganh = $mainframe->getUserStateFromRequest( $option.'filter_nganh', 'filter_nganh', '0', 'string' );
		
		//setState($name,$value) : thiet lap trang thai cua bien co ten $name
		$this->setState('filter_order', $filter_order);
		$this->setState('filter_order_Dir', $filter_order_Dir);
		$this->setState('filter_nganh',$filter_nganh);
	  }	 
	
	function _buildContentOrderBy()
	{
		global $mainframe, $option;
	 
			$orderby = '';
			$filter_order     = $this->getState('filter_order');
			$filter_order_Dir = $this->getState('filter_order_Dir');
	 
			/* Error handling is never a bad thing*/
			if(!empty($filter_order) && !empty($filter_order_Dir) ){
				$orderby = ' ORDER BY '.$filter_order.' '.$filter_order_Dir;
			}

			return $orderby;
	}
	
	//lay du lieu tu cac bang ghi trong csdl
	function getData() 
	{
		// if data hasn't already been obtained, load it
		if (empty($this->_data)) {
			$query = $this->_buildQuery();
			$this->_data = $this->_getList($query, $this->getState('limitstart'), $this->getState('limit'));	
		}
		return $this->_data;
	}  
	
	function _buildQuery() 
	{ 
		$where=array();//tao mang de luu tru cac dieu kien
		$tensv = JRequest::getVar('tensv', '');
		//lay gia tri nhap vao truong tensv, default la '' neu ko co
		if($tensv !='')
			$where[]='tensv like \'%'.$tensv.'%\''; 
		if($this->getState('filter_nganh')!='0')
			$where[]='manganh =\''.$this->getState('filter_nganh').'\'';
		if($tensv !=''||$this->getState('filter_nganh')!='0')
			//implode(): noi phan tu mang where cung voi menh de AND
			$where = ( count( $where ) ? ' WHERE (' . implode( ') AND (', $where ) . ')' : '' );
		$query = ' SELECT * ' 
			   . ' FROM #__cuu_sinh_vien '.$where; 
		return $query; 
	}
	
	function getAllData() 
	{ 
		// Lets load the data if it doesn't already exist 
		if (empty( $this->_allData )) 
		{ 
			$db=& JFactory::getDBO();
			$query = $this->_buildQuery(); 
			$db->setQuery($query);
			$this->_allData = $this->_getList( $query ); 
		} 
		return $this->_allData; 
	}
}

?>